// 员工管理接口
var express = require('express');
var router = express.Router();
const {sqlConnect}= require("../sql/sqlUtil")
const {setResult} = require("../util/utils");

// 查询表格信息
router.get("/", (req, res, next)=> {
    var page = req.query.page - 1;
    var limit = req.query.limit * 1;
    var begin = page * limit;
    try {
      if(req.query.searchParams == undefined){
        // 获取数据总数
        sqlConnect("select count(1) as count from users where is_del=0",(err, data)=> {
            if (err) {
              console.log(err);
            } else {
              var count = data[0].count;
              var sql = "select * from users where is_del=0 order by 1 desc limit ?,?";
              sqlConnect(sql, [begin, limit], (err, data)=> {
                if (err) {
                  console.log(err);
                } else {
                  var json = setResult();
                  json.code = 0;
                  json.count = count;
                  json.data = data;
                  res.send(json);
                }
              })
            }
          })
      }else{
        var result = JSON.parse(req.query.searchParams);
           const name = (result.name.length == 0) ? '%' : result.name,
           role = (result.role.length == 0) ? '%' : result.role;
           // 计算表中符合条件信息数量
           sqlConnect("select count(1) as count from users where is_del=0 and name like '%" + name + "%' and role like '%"+role+"%'",(err, data)=> {
             if (err) {
               console.log(err);
             } else {
               var count = data.length;
               var sql = "select * from users where is_del=0 and name like '%" + name + "%' and role like '%"+role+"%' order by 1 desc limit ?,?";
               sqlConnect(sql, [ begin, limit], (err, data)=> {
                 if (err) {
                   console.log(err)
                 } else {
                   var json = setResult();
                   json.code = 0;
                   json.count = count;
                   json.data = data;
                   res.send(json);
                 }
               })
             }
           })
      }

    }catch (err) {
      console.log(err);
    }
})
// 添加员工信息
router.post("/",  (req, res, next)=> {
    const name=req.body.name,
    gender=req.body.gender,
    username = req.body.username,
    password = req.body.password,
    mobile=req.body.mobile,
    school=req.body.school;
    var sql = "insert into users (name,gender,username,password,mobile,schools,is_del,role) values(?,?,?,?,?,?,0,0)";
    var sqlArr = [name, gender, username,password,mobile,school];
    sqlConnect(sql, sqlArr, (err, data)=> {
      if (err) {
        console.log(err);
      } else {
        var json = setResult();
        json.message = "添加成功";
        res.send(json);
      }
    })
  })
  // 校验学校是否已存在
router.post("/regex",(req, res, next)=> {
    var username = req.body.username;
    sqlConnect("select * from users where is_del=0 and username=?",username,(err, data)=> {
      if (err) {
        console.log(err);
      } else {
        var json = setResult();
        json.code=data.length;
        res.send(json);
      }
    })
  })
// 删除员工信息
router.delete("/", (req, res, next)=> {
    var str = req.body.id;
    var sql = "update users set is_del=1 where id in(?)";
    sqlConnect(sql,str,(err, data)=> {
      if (err) {
        var json = setResult();
        json.msg = "删除失败";
        res.send(json);
      } else {
        var json = setResult();
        json.msg = "删除成功";
        res.send(json);
      }
    })
  })
// 返回编辑行信息
router.post("/search",(req,res,next)=>{
    var id = req.body.id;
    sqlConnect("select * from users where id=?",id,(err,data)=>{
        if (err) {
            console.log(err);
        }else {
            var json=setResult();
            json.count=data.length;
            json.data = data;
            res.send(json);
        }
    })
})
// 修改员工信息
router.put("/",(req,res)=>{
  const name=req.body.name,
  gender=req.body.gender,
  username = req.body.username,
  password = req.body.password,
  mobile=req.body.mobile,
  id = req.body.sid;
  let school=null,
  sql=null,
  sqlArr=null;
  if(req.body.role==1){
    sql="update users set name=?,gender=?,username=?,password=?,mobile=? where id=?";
    sqlArr=[name,gender,username,password,mobile,id]
  }else{
    school=req.body.school;
    sql = "update users set name=?,gender=?,username=?,password=?,mobile=?,schools=? where id=?";
    sqlArr=[name,gender,username,password,mobile,school,id]
  }
    sqlConnect(sql,sqlArr,(err,data)=>{
        if (err) {
            res.send(setResult(500,"服务器内部异常!"));
        }else {
            res.send(setResult(200,"修改成功"));
        }
    })
})
// 修改个人信息
router.put("/selfInfo",(req,res)=>{
  var name=req.body.name;
  var mobile = req.body.mobile;
  var id = req.body.uid;
  var sql = "update users set name=?,mobile=? where id=?";
  var sqlArr=[name,mobile,id];
  sqlConnect(sql,sqlArr,(err,data)=>{
      data=Return.setResult();
      if (err) {
          data.code=500;
          data.message="服务器内部异常!"
          res.send(data);
      }else {
          data.message="修改成功"
          res.send(data);
      }
  })
})
module.exports = router;